<?php

class	Admin	extends	CI_Controller	{

				function	__construct()	{
								parent::__construct();
								$this->load->model('admin_model');
				}

				public	function	index()	{
								$this->listado();
				}

								public	function	listado($msg = '')	{
												$data	=	array('administradores'	=>	$this->admin_model->list_admin());
												if ($msg != ''){
																$data['error'] = 1;
																$data['msg'] = $msg;
												}
												$this->load->view('listados/l_admin',$data);
								}

								public	function	insert($id = 0)	{

								if	($id	==	0)	{
												$data	=	array(
																'id_admin'	=>	$id,
																'email'	=>	'',
																'nombre'	=>	'',
																'pass'	=>	'',
																'error'	=>	0,
																'msg'	=>	''
												);
								}	else	{
												$data	=	$this->admin_model->get_admin($id);
								}
								if	(count($_POST))	{
												$this->form_validation->set_rules('email',	'email',	'required|valid_email');
												$this->form_validation->set_rules('pass',	'pass',	'required');
												$this->form_validation->set_rules('nombre',	'nombre',	'required');
												if	($this->form_validation->run()	==	FALSE)	{
																$data['error'] = 1;
																$this->load->view('add/a_admin',$data);
												}	else	{
																if	($id	==	0)	{
																				$operacion	=	$this->admin_model->add_admin();
																}	else	{
																				$operacion	=	$this->admin_model->update($id);
																}
																if	($operacion)	{
																			$this->listado('Opracion realizada con exito!!');
																}	else	{
																				$data['error']	=	1;
																				$data['msg']	=	'Revise los datos. El usuario puede estar ya dado de alta.';
																				$this->load->view('add/a_admin',$data);
																}
												}
								}	else	{
												$this->load->view('add/a_admin',$data);
								}
				}

				function	eliminar($id)	{
								$this->admin_model->erase($id);
									$this->listado('Usuario eliminado!');
				}
}